-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
140 lines (126 loc) · 3.79 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <vector>
using namespace std;
const int TABLE_SIZE = 10; // You can adjust the table size as needed
// Node structure for linked list
struct Node {
int key;
int value;
Node* next;
Node(int k, int v) : key(k), value(v), next(nullptr) {}
};
class HashMap {
private:
vector<Node*> table; // Array of linked list heads
// Simple hash function: modulo TABLE_SIZE
int hashFunction(int key) {
return key % TABLE_SIZE;
}
public:
HashMap() {
table.resize(TABLE_SIZE, nullptr);
}
// Insert key-value pair; if key exists, update its value.
void insert(int key, int value) {
int index = hashFunction(key);
Node* head = table[index];
// Check if key already exists and update
while (head) {
if (head->key == key) {
head->value = value;
return;
}
head = head->next;
}
// Key not found, so create a new node and insert at beginning
Node* newNode = new Node(key, value);
newNode->next = table[index];
table[index] = newNode;
cout << "Inserted (" << key << " -> " << value << ") into hash map." << endl;
}
// Retrieve value by key; returns true if found and sets the value, false otherwise.
bool get(int key, int &value) {
int index = hashFunction(key);
Node* head = table[index];
while (head) {
if (head->key == key) {
value = head->value;
return true;
}
head = head->next;
}
return false;
}
// Delete a key-value pair from the hash map.
void remove(int key) {
int index = hashFunction(key);
Node* head = table[index];
Node* prev = nullptr;
while (head) {
if (head->key == key) {
if (prev == nullptr)
table[index] = head->next;
else
prev->next = head->next;
delete head;
cout << "Key " << key << " removed." << endl;
return;
}
prev = head;
head = head->next;
}
cout << "Key " << key << " not found." << endl;
}
// Print the entire hash map
void printMap() {
for (int i = 0; i < TABLE_SIZE; i++) {
cout << "Index " << i << ": ";
Node* head = table[i];
while (head) {
cout << "(" << head->key << " -> " << head->value << ") ";
head = head->next;
}
cout << endl;
}
}
};
int main() {
HashMap map;
int choice;
while (true) {
cout << "\nHash Map Menu:\n";
cout << "1. Insert\n";
cout << "2. Get\n";
cout << "3. Delete\n";
cout << "4. Print\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
int key, value;
cout << "Enter key and value: ";
cin >> key >> value;
map.insert(key, value);
} else if (choice == 2) {
int key, value;
cout << "Enter key: ";
cin >> key;
if (map.get(key, value))
cout << "Value for key " << key << " is " << value << endl;
else
cout << "Key not found." << endl;
} else if (choice == 3) {
int key;
cout << "Enter key to delete: ";
cin >> key;
map.remove(key);
} else if (choice == 4) {
map.printMap();
} else if (choice == 5) {
break;
} else {
cout << "Invalid choice. Try again." << endl;
}
}
return 0;
}